package org.framework.lazy.cloud.network.heartbeat.protocol.auth;

import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class DefaultNettyPasswordAuth extends AbstractNettyPasswordAuth {

    protected Map<String/*username*/, String/*password*/> authMap = new ConcurrentHashMap<>();

    protected DefaultNettyPasswordAuth(ProtocolProxyProperties protocolProxyProperties) {
        super(protocolProxyProperties);
    }

    @Override
    public boolean doVerify(String username, String password) {
        boolean verify = authMap.containsKey(username) && authMap.get(username).equals(password);
        if(!verify){
            log.error("授权失败");
        }
        return verify;
    }

    /**
     * 新增授权
     *
     * @param username 用户
     * @param password 密码
     */
    public DefaultNettyPasswordAuth addAuth(String username, String password) {
        authMap.putIfAbsent(username, password);
        return this;
    }
}
